package com.wkbb.basic.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wkbb.basic.feign.DoorFeignService;
import com.wkbb.basic.mapper.DeviceCameraMapper;
import com.wkbb.basic.model.DeviceCamera;
import com.wkbb.basic.service.DeviceCameraService;
import com.wkbb.basic.thread.SynDeviceTask;
import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.common.business.BasicBusiness;
import com.wkbb.common.constants.RedisKeyConstant;
import com.wkbb.common.dto.basic.DeviceCameraDto;
import com.wkbb.common.enums.DoorAreaTypeEnum;
import com.wkbb.common.redis.RedisTemplateUtil;
import com.wkbb.common.utils.StringUtil;
import com.wkbb.common.utils.ThreadPoolUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lilin
 */
@Service
@Slf4j
public class DeviceCameraServiceImpl extends BaseServiceImpl<DeviceCamera> implements DeviceCameraService {

    @Autowired
    private DeviceCameraMapper deviceCameraMapper;

    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    @Autowired
    private DoorFeignService doorFeignService;

    @Autowired
    public void setMapper(DeviceCameraMapper mapper) {
        super.setMapper(mapper);
    }

    @Override
    public void insert(DeviceCamera entity) throws Exception {
        super.insert(entity);
        DeviceCameraDto deviceCameraDto = new DeviceCameraDto();
        BeanUtils.copyProperties(entity, deviceCameraDto);
        redisTemplateUtil.set(BasicBusiness.getIpcRedisKey(entity.getDeviceCode(), entity.getId().toString()), deviceCameraDto);
    }

    @Override
    public void update(DeviceCamera entity) throws Exception {
        DeviceCamera deviceCamera = deviceCameraMapper.getByPrimaryKey(entity.getId());
        super.update(entity);
        DeviceCameraDto deviceCameraDto = new DeviceCameraDto();
        BeanUtils.copyProperties(entity, deviceCameraDto);
//        redisTemplateUtil.set(BasicBusiness.getIpcRedisKey(entity.getDeviceCode(), entity.getId().toString()), deviceCameraDto);

        redisTemplateUtil.delKeysLike(BasicBusiness.getIpcRedisKey("*", entity.getId().toString()));

        //更新设备信息到门禁
        ThreadPoolUtil.addPoolTask(new SynDeviceTask(doorFeignService, entity.getOrgId(),
                deviceCamera.getDeviceCode(), entity.getDeviceCode(), entity.getName(), "", DoorAreaTypeEnum.DORMITORY.getValue().shortValue(), false));
    }

    @Override
    public void deleteCameraBatchByIds(List<Long> ids) {
        for (Long id : ids) {
            DeviceCamera deviceCamera = deviceCameraMapper.getByPrimaryKey(id);
            //更新设备信息到门禁
            ThreadPoolUtil.addPoolTask(new SynDeviceTask(doorFeignService, deviceCamera.getOrgId(), deviceCamera.getDeviceCode(), null, null, ""
                    , null, true));
            redisTemplateUtil.delKeysLike(BasicBusiness.getIpcRedisKey("*", id.toString()));
        }
        deviceCameraMapper.deleteCameraBatchByIds(ids);
    }

    @Override
    public List<DeviceCameraDto> getDeviceCameraList(Long orgId, String orgName, String deviceCode, String name, String serial, Short scene) {
        Map<String, Object> params = new HashMap<>(3);
        if (orgId != null && orgId != 0) {
            params.put("orgId", orgId);
        }
        if (!StringUtil.isNullOrBlank(orgName)) {
            params.put("orgName", orgName);
        }
        if (!StringUtil.isNullOrBlank(deviceCode)) {
            params.put("deviceCode", deviceCode);
        }
        if (!StringUtil.isNullOrBlank(name)) {
            params.put("name", name);
        }
        if (!StringUtil.isNullOrBlank(serial)) {
            params.put("deviceSerial", serial);
        }
        if (scene != null && scene != 0) {
            params.put("scene", scene);
        }

        return deviceCameraMapper.getDeviceCameraList(params);
    }

    @Override
    public PageInfo<DeviceCameraDto> getDeviceCameraPageList(Long orgId, String orgName, String deviceCode, String name, String serial, Short scene, Integer pageNo, Integer pageSize) {
        if (pageNo == null || pageSize == null) {
            return null;
        }

        Page<DeviceCameraDto> page = PageHelper.startPage(pageNo, pageSize, true);
        PageInfo<DeviceCameraDto> pageInfo = new PageInfo<>(getDeviceCameraList(orgId, orgName, deviceCode, name, serial, scene));
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    @Override
    public DeviceCameraDto getDeviceCameraInfo(Long id) {
        Map<String, Object> params = new HashMap<>(1);
        params.put("id", id);

        DeviceCameraDto entity = deviceCameraMapper.getDeviceCameraInfo(params);
        if (entity == null) {
            return null;
        }
        //更新redis
        redisTemplateUtil.set(BasicBusiness.getIpcRedisKey(entity.getDeviceCode(), entity.getId().toString()), entity,RedisKeyConstant.EXPIRATION_TIME_24_HOUR);

        return entity;
    }

    @Override
    public DeviceCameraDto getDeviceCameraInfo(String deviceCode) {
        Map<String, Object> params = new HashMap<>(1);
        params.put("deviceCode", deviceCode);
        DeviceCameraDto entity = deviceCameraMapper.getDeviceCameraInfo(params);
        if (entity == null) {
            return null;
        }
        //更新redis
        redisTemplateUtil.set(BasicBusiness.getIpcRedisKey(entity.getDeviceCode(), entity.getId().toString()), entity,RedisKeyConstant.EXPIRATION_TIME_24_HOUR);

        return entity;
    }
}
